using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class HUDController : MonoBehaviour
{
    [Header("血量")]
    public Image healthSlider; // 血条Slider
    public float oscillationDuration = 0.5f; // 振荡持续时间
    public float oscillationAmount = 0.1f; // 振荡幅度
    [Header("电量")]
    public float Battery = 1;
    public Animator BatteryAnimator;
    [Header("物品栏")] 
    public InventoryElements InventoryElement1;
    public InventoryElements InventoryElement2;
    public InventoryElements InventoryElement3;
    public InventoryElements InventoryElement4;
    
        
    private Coroutine oscillationCoroutine;

    private void Start()
    {
    }

    private void Update()
    {
        UpdateBatteryAnimator();
        // 如果想通过按键测试，可以在这里临时添加按键输入
    }

    // 角色受到伤害时调用的方法
    public void TakeDamage(float damageAmount, float currentHealth, float maxHealth)
    {
        // 计算临时血量
        float tempHealth = Mathf.Max(currentHealth - damageAmount, 0);
        UpdateHealthBar(tempHealth, maxHealth);

        // 启动振荡效果
        if (oscillationCoroutine != null)
        {
            StopCoroutine(oscillationCoroutine);
        }
        oscillationCoroutine = StartCoroutine(OscillateHealthBar(tempHealth, currentHealth, maxHealth));
    }

    // 更新血条UI
    private void UpdateHealthBar(float health, float maxHealth)
    {
        healthSlider.fillAmount = health / maxHealth;
    }

    // 振荡效果的协程
    private IEnumerator OscillateHealthBar(float tempHealth, float actualHealth, float maxHealth)
    {
        float elapsedTime = 0f;
        float startValue = tempHealth / maxHealth;

        while (elapsedTime < oscillationDuration)
        {
            // 计算新的血量值并更新UI
            float oscillatedValue = startValue + Mathf.Sin(elapsedTime * 20f) * oscillationAmount;
            healthSlider.fillAmount = Mathf.Clamp01(oscillatedValue);
            elapsedTime += Time.deltaTime;
            yield return null;
        }

        // 确保最后显示正确的血量
        UpdateHealthBar(actualHealth, maxHealth);
    }

    public void UpdateBattery(float battery)
    {
        Battery = battery;
    }

    public void UpdateBatteryAnimator()
    {
        BatteryAnimator.SetFloat("Blend",Battery);
    }

    public void GetItem(int i, int itemCount)
    {
        switch (i)
        {
            case 1:InventoryElement1.GetItem(itemCount);break;
            case 2:InventoryElement2.GetItem(itemCount);break;
            case 3:InventoryElement3.GetItem(itemCount);break;
            case 4:InventoryElement4.GetItem(itemCount);break;
            default:break;
        }
    }

    public void UseItem(int i,int itemCount)
    {
        switch (i)
        {
            case 1:InventoryElement1.UseItem(itemCount);break;
            case 2:InventoryElement2.UseItem(itemCount);break;
            case 3:InventoryElement3.UseItem(itemCount);break;
            case 4:InventoryElement4.UseItem(itemCount);break;
            default:break;
        }
    }

    public void CoolDownItem(int i, float CD)
    {
        switch (i)
        {
            case 1 :InventoryElement1.CoolDownItem(CD);break;
            case 2 :InventoryElement2.CoolDownItem(CD);break;
            case 3 :InventoryElement3.CoolDownItem(CD);break;
            case 4 :InventoryElement4.CoolDownItem(CD);break;
            default:break;
        }
    }
}